<?php
namespace app\common\logic;

use app\common\model\Picture as PictureModel;
use think\Controller;
use app\common\model\ShopType as ShopTypeModel;
use app\common\model\Shop as ShopModel;
use app\common\model\ShopItem as ShopItemModel;
use app\common\model\ShopTech as ShopTechModel;
use think\Db;


class Shop extends Controller
{
    /**
     * @param int $topid
     * @return false|\PDOStatement|string|\think\Collection
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     * 店铺分类列表
     */
   public function getshoptype($topid=0){

     return  ShopTypeModel::alias('st')
           ->join('t_picture p','st.pictureid = p.id','LEFT')->where("st.topid",$topid)->where("st.status",1)->field("st.id,st.name,p.picurl")->order("st.sort asc")->select();

   }

   public function getarealist($topid=100000,$id=100000){
      if($id){
         return  Db::table("t_area")->where("ID",$id)->select();
      }else{
         return  Db::table("t_area")->where("ParentId",$topid)->select();
      }
     
   }

    /**
     * @param $lat
     * @param $lon
     * @param $raidus
     * @param $typeid
     * @param $start
     * @param $num
     * @return false|\PDOStatement|string|\think\Collection
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     * 店铺列表
     * @return id: 店铺ID
     * @return name: 店铺名称
     * @return address:店铺地址
     * @return areaname:地区名
     * @return thumb:缩略图
     * @return push:地区名
     * @return lat:地区名
     * @return lng:地区名
     * @return tel:电话
     * @return stars:星星
     * @return consumption:人均消费
     * @return consnumber:消费人数
     *
     */

    public function get_shoplist($lat,$lon,$raidus,$typeid=0,$start=1,$num=20,$area_id=0){


        $where=[];
        if($typeid){
        $where['s.type_id']=['eq',$typeid];
        }
         if($area_id){
            $where['s.area_id']=['eq',$area_id];
        }else{

            if($lat&&$lon) {
                $data=getAround($lat, $lon, $raidus);
                $where['s.lng'] = ['between', [$data['minLng'], $data['maxLng']]];
                $where['s.lat'] = ['between', [$data['minLat'], $data['maxLat']]];
            }

       
        }
        $shopdata=ShopModel::alias('s')
            ->join('t_picture p','s.thumb = p.id','LEFT')
            ->join('t_area a','s.area_id=a.ID','LEFT')
            ->where($where)
            ->where("s.status",1)
            ->field("s.id as id,s.area_id,s.name as name,s.address as address,a.Name as areaname,p.picurl as thumb,push ,s.lat,s.lng,s.consumption,s.stars,s.consnumber,s.tel")
            ->order("s.id desc")
            ->page($start,$num)->select();

            if ($shopdata) {
                foreach ($shopdata as $key => $value) {
                    if($lat&&$lon) {
                    $shopdata[$key]['range'] = getDistance3($lat, $lon, $shopdata[$key]['lat'], $shopdata[$key]['lng']);
                    if ($shopdata[$key]['range'] <= 0) {
                        $shopdata[$key]['range'] = 0;
                    }
                    }else{
                        $shopdata[$key]['range'] = 0;
                    }
                   $shopdata[$key]['shopitmes'] = $this->getshopitem($shopdata[$key]['id']);
                }
                $shopdata = array_sort($shopdata, 'range');
            }

         return $shopdata;

    }



    /**
     * @param $shopid
     * @return false|\PDOStatement|string|\think\Collection
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     * 店铺详情
     */
    public function getshopinfo($shopid,$lat,$lon){

        $shopinfo=ShopModel::alias('s')
            ->join('t_picture p','s.thumb = p.id','LEFT')
            ->where("s.id",$shopid)->where("status",1)
            ->field("s.id,name,address,s.lat,s.lng,tel,picture_id,type_id,p.picurl,s.environ_id,s.shophours,s.stars")->find();
        if($lat&&$lon) {
            $shopinfo['range'] = getDistance3($lat, $lon, $shopinfo['lat'], $shopinfo['lng']);
            if ($shopinfo['range'] <= 0) {
                $shopinfo['range'] = 0;
            }
        }else {
            $shopinfo['range'] = 0;
        }
        $shopinfo['picture_array']=PictureModel::where("id","in",explode(",",$shopinfo['picture_id']))->select();
        $shopinfo['environ_array']=PictureModel::where("id","in",explode(",",$shopinfo['environ_id']))->select();



        return $shopinfo;

    }


    /**
     * @param $shopid
     * @return array|false|\PDOStatement|string|\think\Model
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     * 店铺评价
     */
    public function get_praise($shopid,$start=1,$num=20){

        $praiseinfo=\app\common\model\Praise::alias('s')
            ->join('t_user u','s.uid = u.id','LEFT')
            ->where("shopid",$shopid)
            ->page($start,$num)
            ->order("addtime desc")
            ->field("s.*,u.username")->select();
        foreach ($praiseinfo as $key=>$val){
         $pic_array= PictureModel::where("id","in",$praiseinfo[$key]['picture_id'])->field('picurl')->order('addtime desc')->select();
            $praiseinfo[$key]['picture_id']=  $pic_array;
        }
        return $praiseinfo;

    }


    /**
     * @param $shopid
     * @return false|\PDOStatement|string|\think\Collection
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     * 项目列表
     */
    public function getshopitem($shopid,$start=1,$num=20){

        $shop_item_list =  ShopItemModel::alias('s')
            ->join('t_picture p','s.pictureid = p.id','LEFT')->where("sid",$shopid)->where("status",1)->field("s.id,cid,name,content,money,picurl,pictureid,soldnum")->page($start,$num)->order("s.money asc")->select();

        return $shop_item_list;

    }

    /**
     * @param $shopid
     * @return false|\PDOStatement|string|\think\Collection
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     * 项目教师列表
     */
    public function getshopteach($shopid,$start=1,$num=20){
       return ShopTechModel::alias('s')->join('t_picture p','s.thumb = p.id','LEFT')->where("sid",$shopid)->where("status",1)->field("s.id,name,age,sex,phone,picurl,position,goodat")->page($start,$num)->order("s.created_at desc")->select();
    }



}